package com.qf.flash.ui.circle;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.model.RewardInfoBean;
import com.qf.flash.mvp.contract.AbilityContract;
import com.qf.flash.mvp.presenter.AbilityPresenter;
import com.qf.flashlib.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AbilityListActivity extends BaseActivity implements AbilityContract.View {


    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.rb_mp)
    RadioButton rbMp;
    @BindView(R.id.rb_zh)
    RadioButton rbZh;
    @BindView(R.id.rb_yq)
    RadioButton rbYq;
    @BindView(R.id.rb_yz)
    RadioButton rbYz;
    @BindView(R.id.vp_list)
    ViewPager vpList;
    @BindView(R.id.tv_goal)
    TextView tvGoal;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;
    private List<ListFragment> fragments;
    private AbilityPresenter abilityPresenter;
    private Dialog dialog;
    private TextView content;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ability_list;
    }

    @Override
    protected void initInject() {
        super.initInject();
        abilityPresenter = new AbilityPresenter();
        initPresenters(abilityPresenter);
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("能力榜");
        fragments = new ArrayList<>();
        fragments.add(ListFragment.newInstance("0"));
        fragments.add(ListFragment.newInstance("1"));
        fragments.add(ListFragment.newInstance("2"));
        fragments.add(ListFragment.newInstance("3"));

        vpList.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        vpList.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        rgMain.check(R.id.rb_mp);

                        break;
                    case 1:
                        rgMain.check(R.id.rb_zh);

                        break;
                    case 2:
                        rgMain.check(R.id.rb_yq);
                        break;
                    case 3:
                        rgMain.check(R.id.rb_yz);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        vpList.setOffscreenPageLimit(3);
        initDialog();
        abilityPresenter.getRankRewardInfo();
    }

    private void initDialog() {
        dialog = new Dialog(this, R.style.custom_dialog2);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);

        View v = View.inflate(this, R.layout.dialog_reward, null);
        window.setContentView(v);
//        window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
        WindowManager.LayoutParams p = window.getAttributes();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        p.width = (int) (d.getWidth() * 0.82);
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(p);

        content = v.findViewById(R.id.tv_content);

        ImageView close = v.findViewById(R.id.iv_close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });


    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, AbilityListActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_top_back, R.id.rb_mp, R.id.rb_zh, R.id.rb_yq, R.id.rb_yz, R.id.tv_goal})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.rb_mp:
                vpList.setCurrentItem(0);
                break;
            case R.id.rb_zh:
                vpList.setCurrentItem(1);
                break;
            case R.id.rb_yq:
                vpList.setCurrentItem(2);
                break;
            case R.id.rb_yz:
                vpList.setCurrentItem(3);
                break;
            case R.id.tv_goal:// 奖励
                dialog.show();
                break;
        }
    }

    @Override
    public void setRankRewardInfo(RewardInfoBean infoBean) {

        content.setText(infoBean.info);
    }
}
